<?php

class Application_Model_MyProductxMapper {

    protected $_dbTable;

    public function setDbTable($dbTable) {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }

        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }

        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable() {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_MyProductx');
        }

        return $this->_dbTable;
    }

    public function save(Application_Model_MyProductx $productx) {
        $data = array(
            'Product' => $productx->getProduct(),
            'CatId' => $productx->getCat_id(),
            'Cat' => $productx->getCat()
        );

        if (null === ($id = $productx->getId())) {
            unset($data['Id']);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('Id = ?' => $id));
        }
    }

    public function find($id, Application_Model_MyProductx $content) {
        $result = $this->getDbTable()->find($id);

        if (0 == count($result)) {
            return;
        }

        $row = $result->current();
        $content->setId($row->Id)
                ->setProduct($row->Product)
                ->setCat_id($row->CatId)
                ->setCat($row->Cat)
                ->setManufac_id($row->ManufacId)
                ->setManufac($row->Manufac)
                ->setImage($row->Image)
                ->setDescript($row->Descript)
                ->setModel($row->Model)
                ->setMade_in($row->MadeIn)
                ->setSize($row->Size)
                ->setPower($row->Power)
                ->setXuse($row->Xuse)
                ->setWarranty($row->Warranty)
                ->setPrice($row->Price)
                ->setPost_date($row->PostDate)
                ->setPoster($row->Poster)
                ->setTimes($row->Times)
                ->setXshow($row->Xshow);
    }

    public function fetchAll($query = null) {
        $table = $this->getDbTable();
        $select = $table->select();
        $select->from($table)
                ->order("Product asc");
        if (!($query == null)) {
            $select->where($query);
        }
        $select->order("Id asc");

        $resultSet = $table->fetchAll($select);
        return $this->processResultSet($resultSet);
    }

    private function processResultSet($resultSet) {
        $entries = array();

        foreach ($resultSet as $row) {
            $entry = new Application_Model_MyProductx();

            $entry->setId($row->Id);
            $entry->setProduct($row->Product);
            $entry->setCat_id($row->CatId);
            $entry->setCat($row->Cat);
            $entry->setManufac_id($row->ManufacId);
            $entry->setManufac($row->Manufac);
            $entry->setImage($row->Image);
            $entry->setDescript($row->Descript);
            $entry->setModel($row->Model);
            $entry->setMade_in($row->MadeIn);
            $entry->setSize($row->Size);
            $entry->setPower($row->Power);
            $entry->setXuse($row->Xuse);
            $entry->setWarranty($row->Warranty);
            $entry->setPrice($row->Price);
            $entry->setPost_date($row->PostDate);
            $entry->setPoster($row->Poster);
            $entry->setTimes($row->Times);
            $entry->setXshow($row->Xshow);

            $entries[] = $entry;
        }

        return $entries;
    }

    public function getCount() {
        $table = $this->getDbTable();
        $select = $table->select();
        $select->from($table, array("count(Id) as sum"));

        $resultSet = $table->fetchAll($select);
        $array = $resultSet->toArray();

        return $array[0]['sum'];
    }

    public function fetchAllFilterToArray($query) {
        $table = $this->getDbTable();
        $select = $table->select();

        $select->from($table)
                ->where($query);

        $resultSet = $table->fetchAll($select);
        $toArray = $resultSet->toArray();

        return $toArray[0];
    }

    public function delete($id) {
        $table = $this->getDbTable();
        $table->delete("Id = $id");
    }

}